<?php

/**
 * 在线客服
 * File Name：ServiceModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-31 9:27:54
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class ServiceModel extends BaseModel {

    public function tableName() {
        return '{{service}}';
    }

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('name_number', 'required', 'message' => '号码/昵称不能为空！'),
            array('note', 'required', 'message' => '提示信息不能为空！'),
            array('type,style,position,disabled,listorder', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 类型
     * @param type $str 如果是字符串，获取对应键值，如果是数字，获取名称，为空，获取全部(数组)
     * @return array
     */
    public function getTypeArr($str = NULL) {
        $list = array(0 => '旺旺号', 1 => '旺旺群', 2 => 'QQ号码', 3 => 'QQ群号', 4 => '卖家QQ群');
        if ($str === NULL) {
            return $list;
        } else {
            if (is_string($str)) {//传入字符，表示返回id
                return array_search($str, $list);
            } else {
                return isset($list[$str]) ? $list[$str] : false;
            }
        }
        return array();
    }

    /**
     * 显示位置
     * @param type $str
     * @return string
     */
    public function getPositionArr($str = NULL) {
        $list = array(0 => '报名页右侧', 1 => '首页导航位置', 2 => '头部右侧', 3 => '其它', 4 => '商家报名及卖家中心');
        if ($str === NULL) {
            return $list;
        } else {
            if (is_string($str)) {//传入字符，表示返回id
                return array_search($str, $list);
            } else {
                return isset($list[$str]) ? $list[$str] : false;
            }
        }
        return array();
    }

    /**
     * 前台QQ群获取数据
     * @return type
     */
    public function frontCallData() {
        $key = "ServiceModel_frontCallData";
        $data = Yii::app()->cache->get($key);
        if (empty($data)) {
            $where = array('type' => 4, 'order' => 'disabled desc');
            $dataList = $this->findAll($this->where($where));
            if (empty($dataList)) {
                return false;
            }
            $data = array();
            foreach ($dataList as $rs) {
                $data[] = $rs->attributes;
            }
            Yii::app()->cache->set($key, $data, 86400);
        }
        return $data;
    }

}
